use "created_dta/analysis_samp_whole_samp", clear
egen qtr = group(year_arrival quarter_arrival)
	bys dmsl_tc_first qtr : egen meandep = mean(dep_win_3yr)
	bys dmsl_tc_first qtr: egen ct = total(1)
	g insamp_inst_3_yr_allterms = ((meandep*ct) - dep_win_3yr)/(ct-1)
	drop meandep ct 

	gen entry_fy = year(init_entrydt_s)
	replace entry_fy = entry_fy +1 if month(init_entrydt_s) >= 10 & !missing(init_entrydt_s)
g last_useable_date = ym(2019,12) 
g last_useable_date_ssa = ym(2018,9)

g months_observed =  last_useable_date -ym(year_arrival,month_arrival) 
g months_observed_ssa = last_useable_date_ssa -ym(year_arrival,month_arrival)

g num_qtr_allowed =  floor((months_observed)/3)
g num_qtr_allowed_ssa = floor((months_observed_ssa/3))
 

summ months_observed num_qtr_allowed if year_arrival == 2004
summ months_observed num_qtr_allowed if year_arrival == 2005
summ months_observed num_qtr_allowed if year_arrival == 2006
summ months_observed num_qtr_allowed if year_arrival == 2007
summ months_observed num_qtr_allowed if year_arrival == 2008
summ months_observed num_qtr_allowed if year_arrival == 2009
summ months_observed num_qtr_allowed if year_arrival == 2010
summ months_observed num_qtr_allowed if year_arrival == 2011
summ months_observed num_qtr_allowed if year_arrival == 2012
summ months_observed num_qtr_allowed if year_arrival == 2013
summ months_observed num_qtr_allowed if year_arrival == 2014
summ months_observed num_qtr_allowed if year_arrival == 2015
summ months_observed num_qtr_allowed if year_arrival == 2016

summ num_qtr_allowed 
summ num_qtr_allowed_ssa 


egen mos_post_qtr_terms = group(init_pmos_cons post_orgraa year_arrival quarter_arrival init_terms)
egen mos_post_qtr_terms_bct = group(init_pmos_cons post_orgraa year_arrival quarter_arrival init_terms bct_type)
egen mth = group(year_arrival month_arrival)

bys dmsl_tc_first qtr : egen meandep = mean(dep_win_3yr)
bys dmsl_tc_first qtr: egen ct = total(1)

g insamp_inst_3_yr = ((meandep*ct) - dep_win_3yr)/(ct-1)
summ insamp_inst_3_yr
corr insamp_inst_3_yr count_inst_3yr
drop if insamp_inst_3_yr == .

forvalues x = 1(1)36 {
	local w1 = 3*(`x'-1) 
	local w2 = 3*(`x'-1) + 1 
	local w3 = 3*(`x'-1) + 2 

g deployed_q`x' = (deployed_`w1' == 1 | deployed_`w2' == 1 | deployed_`w3' == 1)
}

local everdep deployed_q1==1 
g ever_dep_q1 = deployed_q1==1 
forvalues x = 2(1)36 {
local everdep `everdep' | deployed_q`x' == 1 
g ever_dep_q`x' = `everdep'
}

forvalues x = 1(1)$endx {
   g sep_by_`x' = dur_army >= 0 &  dur_army < 0.25*(`x')
}

g ym_arr = ym(year_arrival,month_arrival) 
cap drop _merge

*merge in VADC data.  
merge 1:1 pid using "raw_dta/VADCpanel_cleaned_wide", keep(1 3) nogen


forvalues x = 1(1)59 {
*vadc amt (need to go farther out for Fig A13)
    gen amtvadc_in_`x' = vadc_gross_amt2001 if (ym_arr + 3*(`x'-1)) >= ym(2004,9) 
	forvalues yy = 2002(1)2018 {
replace  amtvadc_in_`x' = vadc_gross_amt`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
replace amtvadc_in_`x' = vadc_gross_amt2019 if (ym_arr + 3*(`x'-1)) >= ym(2019,9)
replace amtvadc_in_`x' = 0 if amtvadc_in_`x' == .
}

forvalues x = 1(1)$endx {
*any vadc 
    gen anyvadc_in_`x' = any_vadc2001 if (ym_arr + 3*(`x'-1)) >= ym(2001,9) 
	forvalues yy = 2002(1)2018 {
replace  anyvadc_in_`x' = any_vadc`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
replace anyvadc_in_`x' = any_vadc2019 if (ym_arr + 3*(`x'-1)) >= ym(2019,9)
replace anyvadc_in_`x' = 0 if anyvadc_in_`x' == .


*worklim
    gen anywl_in_`x' = vadc_worklim2001 if (ym_arr + 3*(`x'-1)) >= ym(2004,9) 
	forvalues yy = 2002(1)2018 {
replace  anywl_in_`x' = vadc_worklim`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
replace anywl_in_`x' = vadc_worklim2019 if (ym_arr + 3*(`x'-1)) >= ym(2019,9)
replace anywl_in_`x' = 0 if anywl_in_`x' == .


*any ptsd
    gen anyptsd_in_`x' = any_ptsd2001 if (ym_arr + 3*(`x'-1)) >= ym(2004,9) 
	forvalues yy = 2002(1)2018 {
replace  anyptsd_in_`x' = any_ptsd`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
replace anyptsd_in_`x' = any_ptsd2019 if (ym_arr + 3*(`x'-1)) >= ym(2019,9)
replace anyptsd_in_`x' = 0 if anyptsd_in_`x' == .

*any tbi
    gen anytbi_in_`x' = any_tbi2001 if (ym_arr + 3*(`x'-1)) >= ym(2004,9) 
	forvalues yy = 2002(1)2018 {
replace  anytbi_in_`x' = any_tbi`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
replace anytbi_in_`x' = any_tbi2019 if (ym_arr + 3*(`x'-1)) >= ym(2019,9)
replace anytbi_in_`x' = 0 if anytbi_in_`x' == .

}

drop any_vadc* vadc_gross_amt* vadc_worklim* any_ptsd* any_tbi* vadc*

*merge in casualty data
merge 1:1 pid using "raw_dta\cas_unit_deploy_nopii_wide_v3", nogen keep(1 3)

gen cas_death_myr = incident_myr1 if death1 == 1
gen cas_death_country = incident_country1 if death1 == 1
forvalues i = 1/5 {
	gen months_to_incident`i' = incident_myr`i' - ym_arr
	replace cas_death_myr = incident_myr`i' if death`i' == 1
	replace cas_death_country = incident_country`i' if death1 == `i'
}

forvalues x = 1(1)$endx {
	gen wia_by_`x' = months_to_incident1 >= 0 &  months_to_incident1 < 3*(`x') & wia1==1
	gen kia_by_`x' = months_to_incident1 >= 0 &  months_to_incident1 < 3*(`x') & kia1==1

	
	forvalues y = 2/5 {
		replace wia_by_`x' =1 if months_to_incident`y' >= 0 &  months_to_incident`y' < 3*(`x') & wia`y'==1
		replace kia_by_`x' =1 if months_to_incident`y' >= 0 &  months_to_incident`y' < 3*(`x') & kia`y'==1
	}
	
}

*NDI Death Data 
merge 1:1 pid using "created_dta\ndi_death_data_clean_v2", nogen keep(1 3) keepusing(ndi_death_myr ndi_suicide* ndi_assault* ndi_war* ndi_all_acc ndi_firearm* ndi_drug* ndi_narcotic* ndi_alcohol* ndi_mv*  ndi_other ndi_other_2 ndi_und_intent* ndi_despair* ndi_combat*)

format ndi_death_myr %tm

gen months_to_death = ndi_death_myr - ym_arr

local CAUSE despair drug drug_acc drug_self drug_und narcotic narcotic_self narcotic_und alcohol alcohol_acc alcohol_self alcohol_und firearm firearm_acc firearm_self firearm_und  suicide suicide_plus mv mv_acc mv_self mv_und war assault und_intent other combat
forvalues x = 1(1)$endx {
    gen ndi_death_by_`x' = months_to_death >= 0 &  months_to_death < 3*(`x')
	foreach cause in `CAUSE' {
	    gen ndi_`cause'_by_`x' = ndi_death_by_`x' & ndi_`cause'
		gen ndi_`cause'2_by_`x' = ndi_death_by_`x' & ndi_`cause'_2
	}
	gen combat_death_by_`x' = ndi_combat_by_`x' == 1 | kia_by_`x' == 1
} 

drop months_to_death 


drop months_to_incident? incident* wia? kia? suicide? death? serwia? vserwia? any_wia any_kia any_vserwia any_wia  compt?
drop ndi_suicide ndi_suicide_plus ndi_suicide_2 ndi_suicide_plus_2 ndi_drug ndi_drug_acc ndi_drug_self ndi_drug_und ndi_drug_2 ndi_drug_acc_2 ndi_drug_self_2 ndi_drug_und_2 ndi_narcotic ndi_narcotic_acc ndi_narcotic_self ndi_narcotic_und ndi_narcotic_2 ndi_narcotic_acc_2 ndi_narcotic_self_2 ndi_narcotic_und_2 ndi_alcohol ndi_alcohol_acc ndi_alcohol_self ndi_alcohol_und ndi_alcohol_2 ndi_alcohol_acc_2 ndi_alcohol_self_2 ndi_alcohol_und_2 ndi_firearm ndi_firearm_acc ndi_firearm_self ndi_firearm_und ndi_firearm_2 ndi_firearm_acc_2 ndi_firearm_self_2 ndi_firearm_und_2  ndi_mv ndi_mv_acc ndi_mv_self ndi_mv_und ndi_mv_2 ndi_mv_acc_2 ndi_mv_self_2 ndi_mv_und_2 ndi_war_2 ndi_assault ndi_assault_2 ndi_und_intent ndi_und_intent_2 ndi_other ndi_other_2 ndi_despair ndi_despair_2

*Merge ssdi/ssi
merge 1:1 pid using "raw_dta\ssdi_unit_deploy_cleaned_nopii", keep(1 3) nogen
merge 1:1 pid using "raw_dta\ssa_death_ssi_unit_deploy_nopii", keep(1 3) nogen

gen n = 1
merge m:1 n using "raw_dta\cpi_wide", nogen
drop n 

forvalues x = 1/$endx {
	g anyssdi_in_`x' = ANY_DIB1985 if (ym_arr + 3*(`x'-1)) >= ym(1985,9)
	forvalues yy = 1986/2018 {
		replace anyssdi_in_`x' = ANY_DIB`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
	replace anyssdi_in_`x' = 0 if anyssdi_in_`x' == .
}

*ssdi amount
forvalues x = 1/$endx {
	g amtssdi_in_`x' = DIB_TOT1985 if (ym_arr + 3*(`x'-1)) >= ym(1985,9)
	forvalues yy = 1986/2018 {
		replace amtssdi_in_`x' = DIB_TOT`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
	replace amtssdi_in_`x' = 0 if amtssdi_in_`x' == . 
}
drop ANY_DIB* DIB_TOT*

*any ssi/ssi amt
forvalues x = 133/538 {
	gen any_ssi`x' = 1 if (FEDPMT_`x' > 0 & FEDPMT_`x' != .) | (STATPMT_`x' >0 & STATPMT_`x' !=.)
	gen amt_ssi`x' = FEDPMT_`x' + STATPMT_`x'
	local y = ceil(`x'/12) + 1973
	replace amt_ssi`x' = amt_ssi`x'*(258.844/cpiaucsl`y')
}
forvalues q = 1/135 {
	local m = 3*`q'+130
	local n = `m' + 1
	local j = `m' + 2
	gen any_ssiq`q' = 1 if any_ssi`m' == 1 | any_ssi`n' == 1 | any_ssi`j' == 1
	gen amt_ssiq`q' = amt_ssi`m' + amt_ssi`n' + amt_ssi`j'
}
forvalues x = 1/$endx {
	gen anyssi_in_`x' = any_ssiq1 if (ym_arr + 3*(`x'-1)) >= ym(1985,1)
	gen amtssi_in_`x' = amt_ssiq1 if (ym_arr + 3*(`x'-1)) >= ym(1985,1)
	forvalues q = 2/135 {
		replace anyssi_in_`x' =  any_ssiq`q' if (ym_arr + 3*(`x'-1)) >= (3*`q'+297)
		replace amtssi_in_`x' =  amt_ssiq`q' if (ym_arr + 3*(`x'-1)) >= (3*`q'+297)
	}
	replace anyssi_in_`x' = 0 if anyssi_in_`x' == .
	replace amtssi_in_`x' = 0 if amtssi_in_`x' == .
}

drop FEDPMT_* STATPMT_* PSTAT_* any_ssi* amt_ssi* 

*Disability Outcomes
forvalues x = 1/$endx { 
	gen anydisability_in_`x' = 0
	replace anydisability_in_`x' = 1 if anyssdi_in_`x' == 1 | anyssi_in_`x' == 1 | anyvadc_in_`x' == 1
	gen anysig_disability_in_`x' = 0
	replace anysig_disability_in_`x' = 1 if anyssdi_in_`x' == 1 | anyssi_in_`x' == 1 | anywl_in_`x' == 1
	gen amtdisability_in_`x' = amtssdi_in_`x' + 4*amtssi_in_`x' + amtvadc_in_`x'
	replace amtdisability_in_`x' = 0 if amtdisability_in_`x' == .
}

*Merge Waivers
merge 1:1 pid using "raw_dta\waivers_unit_deploy_wide_nopii", nogen keep(1 3)

	gen waiver = waiver1 == 1
	gen waiver_moral =  waiver_moral1 == 1
	gen waiver_moral_felon = waiver_moral_felony1 == 1 | waiver_moral_felony_adult1 == 1
	gen waiver_drug =waiver_drug1 == 1

	gen waiver_prsvc= waiver_prsvc1 ==1 
	gen waiver_medical= waiver_medical1 ==1

forvalues y = 1/21 {
	replace waiver =1 if waiver`y' == 1
	replace waiver_moral =1 if  waiver_moral`y' == 1
	replace waiver_moral_felon =1 if waiver_moral_felony`y' == 1 | waiver_moral_felony_adult`y' == 1
	replace waiver_drug =1 if waiver_drug`y' == 1

	replace waiver_prsvc=1 if waiver_prsvc`y' ==1 
	replace waiver_medical=1 if waiver_medical`y' ==1
	}

gen any_moral_waiver = waiver_moral ==1 | waiver_moral_felon == 1 | waiver_drug == 1
gen no_moral_waiver = !any_moral_waiver

*MERGE ADDITIONAL X
merge 1:1 pid using ${rawdataorig}/mepcom_last_unit_deploy_nopii, gen(_merge_mepcom) keep(1 3)

gen any_meps_med_fail_cd = mep_meps_med_fail_cd_1 != "" | mep_meps_med_fail_cd_2 != ""

gen any_meps_drug_tst_miss = 0
foreach drug in alcohol marijuana cocaine {
	gen any_meps_`drug'_fail = !inlist(mep_`drug'_test_result,"N","Z","")
	gen any_meps_`drug'_tst_miss = inlist(mep_`drug'_test_result,"Z","")
	replace any_meps_drug_tst_miss = 1 if inlist(mep_`drug'_test_result,"Z","")
}
gen any_meps_drug_fail = max(any_meps_alcohol_fail,any_meps_marijuana_fail,any_meps_cocaine_fail)

preserve
use  ${rawdataorig}/ace_first_v2_unit_deploy_nopii, clear
foreach x in asvabap asvabcl asvabco asvabel asvabfa asvabgm asvabgt asvabmm asvabof asvabsc asvabtest ngibstat religcat statebrth filedt_s asvabdt_s ngibdt_s {
	rename `x' ace_first_`x'
}
tempfile ace_first
save `ace_first'
restore
merge 1:1 pid using `ace_first', nogen keep(1 3)

*GEN SPLITS
g init_white = init_race == "W"
g init_black = init_race == "B"
g init_hispanic = init_race == "H"
g init_other_race = init_race == "X"
g init_afqsc_abv50 = init_afqsc >= 50 & init_afqsc != .
g init_afqsc_bel50 = init_afqsc < 50
g init_mos11 = substr(init_pmos_cons,1,2) == "11"
g combat_occ = substr(init_pmos_cons,1,2) == "11" | substr(init_pmos_cons,1,2) == "13" | substr(init_pmos_cons,1,2) == "14" | substr(init_pmos_cons,1,2) == "18" | substr(init_pmos_cons,1,2) == "19" | init_pmos_cons == "12B"
g noncombat_occ = !combat_occ
g hsd_ged = init_civedcatg == "HSD" | init_civedcatg == "GED"
g high_school = init_civedcatg == "HSG"
g college = init_civedcatg == "ASC" | init_civedcatg == "CLG" | init_civedcatg == "SMC" | init_civedcatg == "GRD"
g not_init_mos11 = substr(init_pmos_cons,1,2) != "11"
g early_cohort = inrange(year_arrival,2005,2010)
g late_cohort = inrange(year_arrival,2011,2015)

*save dataset 
keep pid wia_by_* kia_by_* anyvadc_in_* amtvadc_in_* anyssdi_in_* anyptsd_in_* anytbi_in_* anywl_in_* amtdisability_in_* anydisability_in_* num_qtr_allowed* black hisp other_race female married anydep hsgplus init_approx_age bct_type mos_post_qtr_terms init_pmos_cons post_orgraa init_terms dep_win_3yr count_inst_3yr insamp_inst_3_yr init_white init_black init_hispanic init_other_race init_afqsc_abv50 init_afqsc_bel50 init_afqsc init_mos11 not_init_mos11 combat_occ noncombat_occ hsd_ged high_school college any_moral_waiver no_moral_waiver dmsl_tc_first afqs_mi ever_dep_q* qtr early_cohort late_cohort any_hfp_ever months_hfp_ever deployed_* year_arrival month_arrival BCTXarr* BCT months_hfp_3yr ndi* init_nrdep* anydep ndi_war ndi_death_myr init_entrydt_s entry_fy   init_* mep_* any_mep* ace_first_* ap_m_eboa ap_m_ebtot ap_m_ebtp ap_m_srboa ap_m_srbtot ap_m_srbtp ap_filedt_s ap_mgibillstat _merge_mepcom  combat_death_by_*

preserve
use created_dta/analysis_samp_w_outcomes, clear
gen mths_dep_win_3yr = deployed_0 + deployed_1 + deployed_2 + deployed_3 ///
+ deployed_4 + deployed_5 + deployed_6 + deployed_7 ///
+ deployed_8 + deployed_9 + deployed_10 + deployed_11 ///
+ deployed_12 + deployed_13 + deployed_14 + deployed_15 ///
+ deployed_16 + deployed_17 + deployed_18 + deployed_19 ///
+ deployed_20 + deployed_21 + deployed_22 + deployed_23 ///
+ deployed_24 + deployed_25 + deployed_26 + deployed_27 ///
+ deployed_28 + deployed_29 + deployed_30 + deployed_31 ///
+ deployed_32 + deployed_33 + deployed_34 + deployed_35 
keep pid dmsl_tc_first qtr mths_dep_win_3yr init_pmos_cons post_orgraa init_terms year_arrival month_arrival
foreach x in dmsl_tc_first qtr mths_dep_win_3yr init_pmos_cons post_orgraa init_terms year_arrival month_arrival {
	gen `x'_as = `x'
}
drop mths_dep_win_3yr
tempfile analysis_samp
save `analysis_samp'
restore

merge 1:1 pid using `analysis_samp', gen(_merge_analysis) keep(1 2 3)
compress
save created_dta/whole_samp_w_outcomes, replace
